package com.bocai.yuweiwangluo.activity;


import android.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.navi.BaiduMapAppNotSupportNaviException;
import com.baidu.mapapi.navi.BaiduMapNavigation;
import com.baidu.mapapi.navi.NaviParaOption;
import com.baidu.mapapi.utils.OpenClientUtil;
import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.utils.MyConst;
import com.lidroid.xutils.ViewUtils;

import java.io.File;

/**
 * 地图界面
 */
public class BaiduMapActivity extends BaseActivity{
    //地图视图
    private BaiduMap mBaiduMap;
    private MapView mMapView;
    private String local_x;
    private String local_y;
    @Override
    protected int contentView()
    {
        return R.layout.activity_map;
    }
    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        initToolbar("场地");
        local_x=getIntent().getStringExtra("local_x");
        local_y=getIntent().getStringExtra("local_y");
        mToolbar.inflateMenu(R.menu.menu_navigation);
        initEvent();
        initMap();
    }

    @Override
    protected void initEvent() {
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
        {
            @Override
            public boolean onMenuItemClick(MenuItem item)
            {
                try {

                    if(isInstallByread("com.baidu.BaiduMap"))
                    {
                        startNavi();
                        Log.e("GasStation", "百度地图客户端已经安装") ;
                    }else
                    {
                        showDialog();
                        Log.e("GasStation", "没有安装百度地图客户端") ;
                    }
                } catch (Exception e)
                {
                }
                return true;
            }
        });
    }

    private boolean isInstallByread(String packageName)
    {
        return new File("/data/data/" + packageName).exists();
    }
    /**
     * 启动百度地图导航(Native)
     *
     */
    public void startNavi()
    {
        LatLng pt1 = new LatLng(MyConst.location_lat,MyConst.location_lng);
        LatLng pt2 = new LatLng(Double.parseDouble(local_y), Double.parseDouble(local_x));

        // 构建导航参数
        /**
         * endName(java.lang.String endName):导航终点名称
         * endPoint(LatLng endPoint):导航终点， 百度经纬度坐标
         * startName(java.lang.String startName):导航起点名称
         * startPoint(LatLng startPoint):导航起点， 百度经纬度坐标
         * */
//        NaviParaOption para = new NaviParaOption().startPoint(pt1).endPoint(pt2)
//                .startName("天安门").endName("百度大厦");
        NaviParaOption para = new NaviParaOption().startPoint(pt1).endPoint(pt2);


        try
        {
            /**
             * openBaiduMapNavi(NaviParaOption para, Context context)
             * 调起百度地图导航页面
             * */
            BaiduMapNavigation.openBaiduMapNavi(para, this);
        } catch (BaiduMapAppNotSupportNaviException e)
        {
            e.printStackTrace();
        }
    }
    /**
     * 启动百度地图导航(Web)
     */
    public void startWebNavi()
    {
        LatLng pt1 = new LatLng(MyConst.location_lat,MyConst.location_lng);
        LatLng pt2 = new LatLng(Double.parseDouble(local_y), Double.parseDouble(local_x));
        // 构建 导航参数
        NaviParaOption para = new NaviParaOption()
                .startPoint(pt1).endPoint(pt2);
        /**
         * openWebBaiduMapNavi(NaviParaOption para, Context context)
         * 调起百度地图导航页面
         * */
        BaiduMapNavigation.openWebBaiduMapNavi(para, this);
    }
    /**
     * 提示未安装百度地图app或app版本过低
     *
     */
    public void showDialog()
    {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_openbaidunav, null);
        final AlertDialog dialog = new AlertDialog.Builder(this)
                .setView(view)
                .show();
        view.findViewById(R.id.tv_true).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dialog.dismiss();
                /**
                 * OpenClientUtil:调起百度客户端工具类
                 *
                 * public static int getBaiduMapVersion(Context context)
                 * 获取百度地图客户端版本号
                 * 返回0代表没有安装百度地图客户端
                 * */
                OpenClientUtil.getLatestBaiduMapApp(BaiduMapActivity.this);

            }
        });
        view.findViewById(R.id.tv_openweb).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dialog.dismiss();
                startWebNavi();
            }
        });
        view.findViewById(R.id.tv_dialog_dismiss).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dialog.dismiss();
            }
        });

    }
    private void initMap()
    {
        mMapView=(MapView)findViewById(R.id.mymap);
        mBaiduMap = mMapView.getMap();
        //普通地图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        //定义Maker坐标点
        LatLng point = new LatLng(MyConst.location_lat,MyConst.location_lng);
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.mipmap.icon_marka);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);
        //更新图层数据执行刷新后生效
        //定义地图状态
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(point)
                .zoom(15)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化

        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
        mBaiduMap.setMapStatus(mMapStatusUpdate);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        mMapView.onDestroy();
    }
    @Override
    protected void onPause()
    {
        super.onPause();
        mMapView.onPause();
    }
    @Override
    protected void onResume(){
        super.onResume();
        mMapView.onResume();
    }


}
